#include "cpu/exec/template-start.h"

#define instr ret

make_helper(concat(ret_n_, SUFFIX)){
    cpu.eip = MEM_R(reg_l(R_SP),2);
    if(DATA_BYTE == 2) cpu.eip &= 0xffff;
    reg_l(R_SP) += DATA_BYTE;
    print_asm("ret");
    return 1;
}

make_helper(concat(ret_i_, SUFFIX)){

	cpu.eip = MEM_R (cpu.esp, 2);
	if (DATA_BYTE == 2) cpu.eip &= 0xffff;
	cpu.esp += DATA_BYTE;
    int val = instr_fetch(eip + 1, 2);
	int i;
	for (i = 0; i < val; i += DATA_BYTE) MEM_W (cpu.esp + i,0, 2);//初始化成0
	cpu.esp += val;

	print_asm("ret $0x%x",val);
	return 1;   //尽管实际长度是3字节，但是有关eip的赋值，还是应该返回1
}

//****
// make_helper(concat(ret_n_, SUFFIX)){
//     uint32_t res = swaddr_read(cpu.esp, DATA_BYTE);
//  	cpu.esp += DATA_BYTE;
//  	cpu.eip = res - 1;
//     if(DATA_BYTE == 2) cpu.eip &= 0xffff;
//  	print_asm("ret");
//  	return 1;
// }

// make_helper(concat(ret_i_, SUFFIX)){
//     uint32_t res = swaddr_read(cpu.esp, DATA_BYTE);
// 	cpu.esp += DATA_BYTE;
// 	cpu.eip = res - 1;
//     if(DATA_BYTE == 2) cpu.eip &= 0xffff;
// 	cpu.esp += 2;
// 	print_asm("ret 16");
// 	return 1;
// }

#include "cpu/exec/template-end.h"